package kk.yiyi.safedesign.main;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Window;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import kk.yiyi.safedesign.R;
import kk.yiyi.safedesign.base.Constants;
import kk.yiyi.safedesign.base.MessageEvent;
import kk.yiyi.safedesign.base.SharePresenterUtils;
import kk.yiyi.safedesign.databinding.ActivityMainBinding;
import kk.yiyi.safedesign.gps.GpsUtils;
import kk.yiyi.safedesign.mine.MineFragment;
import kk.yiyi.safedesign.sms.SmsUtils;
import kk.yiyi.safedesign.warming.WarmUtils;
import kk.yiyi.safedesign.warming.WarmingService;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener{
    ActivityMainBinding mainBinding;
    /**
     * sim监听是否已注册
     */
    private boolean hasRegister = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        mainBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(mainBinding.getRoot());
        //gps监控
        GpsUtils.getInstance().getGps(MainActivity.this);
        SmsUtils.requestPermission(this);

        EventBus.getDefault().register(this);
        WarmingService warmingService=new WarmingService();
        warmingService.warmSelect(this);
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView(){
        /** 导航基础设置 包括按钮选中效果 导航栏背景色等 */
        mainBinding.bottomBar
                .setTabSelectedListener(MainActivity.this)
                .setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT)
                .setActiveColor("#fc773c") //选中颜色
                .setInActiveColor("#FFA54F") //未选中颜色
                .setBarBackgroundColor("#ffffff");//导航栏背景色

        /** 添加导航按钮 */
        mainBinding.bottomBar
                .addItem(new BottomNavigationItem(R.drawable.home_icon, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.mine_icon, "我的"))
                .setFirstSelectedPosition(0)
                .initialise(); //initialise 一定要放在 所有设置的最后一项

        setDefaultFragment();//设置默认页

    }

    /**
     * 设置默认导航栏
     */
    private void setDefaultFragment() {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        MainFragment mainFragment=MainFragment.getInstance();
        transaction.replace(R.id.main_content, mainFragment);
        transaction.commit();
    }



    @Override
    public void onTabSelected(int position) {
        Log.d("mydesign_debug", "onTabSelected() called with: " + "position = [" + position + "]");
        FragmentManager fm = this.getFragmentManager();
        //开启事务
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                Log.d("mydesign_debug", "首页");
                transaction.replace(R.id.main_content, MainFragment.getInstance());
                break;
            case 1:
                Log.d("mydesign_debug", "我的");
                transaction.replace(R.id.main_content, MineFragment.getInstance());
                break;

            default:
                break;
        }

        transaction.commit();// 事务提交
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 200://刚才的识别码
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED){//用户同意权限,执行我们的操作
                    //gps监控
                    GpsUtils.getInstance().getGps(MainActivity.this);
                }else{//用户拒绝之后,当然我们也可以弹出一个窗口,直接跳转到系统设置页面
                    Toast.makeText(MainActivity.this,"未开启定位权限,请手动到设置去开启权限",Toast.LENGTH_LONG).show();
                }
                break;
            case 100:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    SmsUtils.sendSMSS(SharePresenterUtils.getData(MainActivity.this, Constants.TABLE,"user"),"这贼人换手机了");
                } else {
                    // Permission Denied
                    SmsUtils.requestPermission(MainActivity.this);
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(MessageEvent message) {

        if(message.getMessage().equals("warming")){
            WarmUtils.getInstance().startWarming(MainActivity.this);
        }
    }
}
